{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "First, we'll import the `pywwt` modules, as well as `numpy` and `h5py`, which we'll need for this example. We also define a conversion between centimeters and megaparsecs, `cm_per_mpc`."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from pywwt.mods import *\n",
      "import numpy as np\n",
      "import h5py\n",
      "cm_per_mpc = 3.0856e24"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Next, we'll connect to a WWT client. This one happens to be running on a separate machine on my network, so I'll specify the IPv4 address of that machine as the host."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "my_wwt = WWTClient(host=\"192.168.1.3\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now, we'll create a layer which we'll put data in later. Since this is tracer particle data from a radio minihalo simulation of a galaxy cluster core, we'll put the layer in the `\"Sky\"` frame and name it `\"minihalo\"`. We also need to specify the fields that will go into the layer, which in this case are just the spherical coordinates of the points and the color of the point. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "my_layer = my_wwt.new_layer(\"Sky\",\"minihalo\",[\"RA\",\"DEC\",\"ALT\",\"color\"])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We need to set the properties of this layer as well. We will construct a `props_dict` to hold the layer properties we want. A summary:\n",
      "\n",
      "- The coordinates are spherical.\n",
      "- The size of the point is relative to the screen.\n",
      "- The points have all the same scale.\n",
      "- The scale of the points is 16. \n",
      "- The points are lit on all sides. \n",
      "- This is not a time series dataset. \n",
      "- The \"altitude\" coordinate (or radius) is in megaparsecs.\n",
      "- The units of the RA coordinate are in degrees. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "props_dict = {\"CoordinatesType\":\"Spherical\",\n",
      "              \"MarkerScale\":\"Screen\",\n",
      "              \"PointScaleType\":\"Constant\",\n",
      "              \"ScaleFactor\":\"16\",\n",
      "              \"ShowFarSide\":\"True\",\n",
      "              \"TimeSeries\":\"False\",\n",
      "              \"AltUnit\":\"MegaParsecs\",\n",
      "              \"RaUnits\":\"Degrees\"}\n",
      "my_layer.set_properties(props_dict)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Though it probably already is, this call activates the layer in the view: "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "my_layer.activate()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now we have to do some work to get the data out of the HDF5 file it's contained in. The data consists of a set of points, with cartesian coordinates in centimeters, and radio emissivity in cgs units. We'll map the radio emissivity to a color map from Matplotlib, and convert the coordinates to spherical. We'll put all of this in a dict, `data`. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fn = \"radio_halo_1kpc_hdf5_part_0200_reduced.h5\"\n",
      "f = h5py.File(fn, \"r\")\n",
      "x = f[\"x\"][:]/cm_per_mpc # The coordinates in the file are in cm, this converts them to Mpc\n",
      "y = f[\"y\"][:]/cm_per_mpc\n",
      "z = f[\"z\"][:]/cm_per_mpc\n",
      "c = f[\"radio\"][:]\n",
      "color = map_array_to_colors(c, \"spectral\", scale=\"log\", vmin=1.0e-40, vmax=4.0e-23)\n",
      "data = convert_xyz_to_spherical(x, y, z)\n",
      "data[\"color\"] = color\n",
      "f.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now we add this data in. We set `purge_all=True` to eliminate the data already in the layer (though it was empty so it's superfluous), and we set the `fly_to` parameter to fly to a particular location and zoom setting relative to the `\"Sky\"` frame:\n",
      "\n",
      "- Latitude: 48 degrees\n",
      "- Longitude: -12 degress\n",
      "- Zoom: $6 \\times 10^{11}$\n",
      "- Rotation: 0 radians\n",
      "- Angle: 0 radians"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "my_layer.update(data=data, purge_all=True, fly_to=[48.,-12.,6.0e11,0.,0.])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Just as a check, we can get the state of the current view (after the fly-to stops) and see that it matches up with the coordinates of our `fly_to` parameter:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "my_wwt.get_state()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "{'angle': '0',\n",
        " 'lat': '48',\n",
        " 'lng': '-12',\n",
        " 'lookat': 'SolarSystem',\n",
        " 'referenceframe': 'Sun',\n",
        " 'rotation': '0',\n",
        " 'time': '1/22/2014 11:05:32 PM',\n",
        " 'timerate': '1',\n",
        " 'viewtoken': 'GK484GJ28CH2E59766142GGGGIC8427AA1468BBD2D453FB0A22FA365486C3F21FB521FD2E8683FGGG',\n",
        " 'zoom': '600000000000',\n",
        " 'zoomtext': '1.2 Mpc'}"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}